package com.my.crawler;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.edu.hfut.dmic.webcollector.crawler.DeepCrawler;
import cn.edu.hfut.dmic.webcollector.model.Links;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.net.HttpRequesterImpl;

public class WeiboCommentCrawler extends DeepCrawler {

	private String cookie;
    public WeiboCommentCrawler(String crawlPath,String cookie) {
        super(crawlPath);

        this.cookie=cookie;
        
      HttpRequesterImpl mhri=(HttpRequesterImpl)this.getHttpRequester();
      mhri.setCookie(cookie);
      
    }

    @Override
    public Links visitAndGetNextLinks(Page page) {
    	
    	String url=page.getUrl();
    	int first=url.indexOf("//");

    	int second=url.indexOf("/",first+1);
    	int third=url.indexOf("/",second+1);
    	int fourth=url.indexOf("/",third+1);
    	int fif=url.indexOf("?",fourth+1);
    	String wbId=url.substring(fourth+1, fif);
    	Elements weibos=page.getDoc().select("div.c");  
        for(Element weibo:weibos){
        	int anum=weibo.getElementsByTag("a").size();
        	if(anum!=1&&anum!=0)
        	{
        		String CmUser=weibo.getElementsByTag("a").get(0).text();
            	String CmTime=MyFuntions.WeiBoHandlerTime(weibo.select("span.ct").text());
            	String CmContent=weibo.select("span.ctt").text();
            	
            	System.out.println(wbId+","+CmUser+","+CmContent+","+CmTime);
        	}
        	
        	try{
        		Thread.sleep(1*1000);
        	}catch(Exception e)
        	{
        		e.printStackTrace();
        	}
        
        }


      

        return null;
    }


}